package priv.pfz.sbv.spring;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;

import java.lang.reflect.Field;

/**
 * @author yyjxp
 * 2024/2/15 1:12
 */
public class SbvApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        try {
            if (applicationContext instanceof GenericApplicationContext) {
                System.out.println("SbvApplicationContextInitializer run");
                SbvBeanFactory beanFactory = new SbvBeanFactory();
                Field field = GenericApplicationContext.class.getDeclaredField("beanFactory");
                field.setAccessible(true);
                field.set(applicationContext, beanFactory);
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
